//
//  ZMWelcomeViewController.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/7/25.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

import SDWebImage


class ZMWelcomeViewController: UIViewController {

    //定义私有控件属性
     let bgImageView = UIImageView(image: #imageLiteral(resourceName: "ad_background"))
    //定义用户头像图片view,由于要设置网络图片和占位图片,这里使用懒加载比较合适
    lazy var  userIconView : UIImageView = {
       
        let imgview = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
        
        //获取用户头像图片的地址
        guard  let imageUrlstr = ZMUserAccountViewModel.sharedViewModel.userAccountModel?.avatar_large else{
            
            print("用户信息不存在")
            return imgview
        }
        //到这里说明用户信息存在
        imgview.sd_setImage(with: URL(string:imageUrlstr), placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
        //设置imageview的圆角
        imgview.layer.cornerRadius = 45
        imgview.layer.masksToBounds = true
        
        
        return imgview

    }()
    
    //懒加载messagelabel
    lazy var messageLabel : UILabel = {
        
        let label = UILabel()
        //获取用户名
        if let name = ZMUserAccountViewModel.sharedViewModel.userAccountModel?.name{
            label.text = "欢迎回来,\(name)"
        }else{
            label.text = "欢迎回来"
        }
        
        label.alpha = 0
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.darkGray
        return label
        
    }()
    
    //自定义跟视图
    override func loadView() {
        view = bgImageView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
        
        setupUI()
    }

    //添加子视图
    func setupUI(){
        view.addSubview(userIconView)
        //设置约束
        userIconView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(200)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        //加载messagelabel
        view.addSubview(messageLabel)
        //设置约束
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userIconView.snp.bottom).offset(10)
            make.centerX.equalTo(userIconView)
        }
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        startAnimation()
    }
    
    //MARK:- 添加动画
    func startAnimation(){
        
        //跟新头像view的约束,实现动画效果
        userIconView.snp.updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 50, options: [], animations: {
            
            self.view.layoutSubviews()
            
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1.0
                
            }, completion: { (_) in
                
                //动画结束切换控制器
                NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotificationName), object: nil)
                
            })
            
        }
        
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
